package com.baidumap.administrator.happystudy.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.ab.view.simple_rating_bar.SimpleRatingBar;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.module.shopcar.MainActivity;
import com.baidumap.administrator.happystudy.utils.GlideUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.baidumap.administrator.happystudy.R.id.simpleRatingBar;

@SuppressWarnings("all")
public class SupermarketAdapter extends BaseAdapter {
    private List<Map<String, Object>> lists;
    private Context context;
    private ViewHolder holder;
    private LayoutInflater inflater;
    private ImageLoader loader;
    private DisplayImageOptions options;
    public ArrayList<A> list;

    public SupermarketAdapter(Context _Context, ImageLoader loader) {

        this.context = _Context;
        this.loader = loader;

    }

    public void bindData(List<Map<String, Object>> list) {
        this.lists = list;
    }


    class ViewHolder {
        LinearLayout list_lay;

        TextView tv_shop_name;
        TextView tv_shop_distance;
        TextView tv_shop_walk_time;
        ImageView img_shop;
        RatingBar ratingBar_shop;
        SimpleRatingBar simpleRatingBar;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public List<Map<String, Object>> getItem(int position) {
        return (List<Map<String, Object>>) lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final int index = position;
        String shop_id = null;
        String shop_name = null;
        if (convertView != null) {

            holder = (ViewHolder) view.getTag();

        } else {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_supermarket, null);
            holder = new ViewHolder();

            holder.list_lay = (LinearLayout) convertView.findViewById(R.id.list_lay);
            holder.tv_shop_name = (TextView) convertView.findViewById(R.id.tv_shop_name);
            holder.img_shop = (ImageView) convertView.findViewById(R.id.img_shop);
            holder.ratingBar_shop = (RatingBar) convertView.findViewById(R.id.ratingBar_shop);
            holder.tv_shop_distance = (TextView) convertView.findViewById(R.id.tv_shop_distance);
            holder.tv_shop_walk_time = (TextView) convertView.findViewById(R.id.tv_shop_walk_time);
            holder.simpleRatingBar = (SimpleRatingBar) convertView.findViewById(simpleRatingBar);


            holder.tv_shop_name.setText((String) lists.get(position).get("shop_name"));
            holder.ratingBar_shop.setNumStars(2);
            holder.simpleRatingBar.setRating(Float.parseFloat((String) lists.get(position).get("shop_rating")));
            holder.simpleRatingBar.setClickable(false);
            holder.tv_shop_distance.setText("距离" + lists.get(position).get("shop_distance").toString() + "公里");
            holder.tv_shop_walk_time.setText(lists.get(position).get("shop_walk_time").toString() + "分钟送达");

            shop_id = (String) lists.get(position).get("shop_id");
            shop_name = (String) lists.get(position).get("shop_name");
            GlideUtil.displayImg(context, lists.get(position).get("shop_img").toString(), holder.img_shop);
            convertView.setTag(holder);
        }
        final String finalShop_id = shop_id;
        final String finalShop_name = shop_name;
        holder.list_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bundle bundle = new Bundle();
                bundle.putString("shop_id", finalShop_id);
                bundle.putString("title", finalShop_name);
                Intent it = new Intent(context, MainActivity.class);
                it.putExtras(bundle);
                context.startActivity(it);
            }
        });


        return convertView;
    }

    public class A {

        public static final int TYPE_CHECKED = 1;
        public static final int TYPE_NOCHECKED = 0;

        public boolean type;
        public String phone;

        public A(boolean type, String phone) {
            this.type = type;
            this.phone = phone;
        }
    }


}
